//
//  CommentViewController.swift
//  iModern
//
//  Created by 立雄 王 on 15/8/14.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class CommentViewController: ViewController, UITextFieldDelegate
{

    let __avatar_image_view_tags = 60
    var commentTextField: UITextField!
    var comment                  = NSDictionary()
    var articleId:        String!
    let appDelegate              = UIApplication.sharedApplication().delegate as! AppDelegate
    var button                   = UIButton()
    var button2                  = UIButton()
    var button3                  = UIButton()
    var button4                  = UIButton()
    var reviewAppointment        = NSMutableDictionary()
    var appointmentManager       = AppointmentManager()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.navigationItem.title = "评价"
        self.view.backgroundColor = UIColor.groupTableViewBackgroundColor()

        initBackButton()
        initRightBarButtonItem()
        initView1()
        initLabel()
        initView2()
    }

    func initView1()
    {
        let screen = UIScreen.mainScreen().bounds
        let view1  = UIView()
        view1.frame = CGRectMake(0, 0, screen.size.width, 50)
        view1.backgroundColor = UIColor.whiteColor()

        let advertising  = comment["advertising"] as! [String:AnyObject]
        let contact      = advertising["contact"] as! [String:AnyObject]
        let thumbnailURL = contact["thumbnailURL"] as? String

        var imageView      = UIImageView()
        let imageViewFrame = CGRect(x: 10.0, y: 5.0, width: 40.0, height: 40.0)
        imageView = UIImageView(frame: imageViewFrame)
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
        else
        {
            imageView.image = UIImage(named: "Avatar-Default.png")
        }
        imageView.layer.cornerRadius = 20.0
        imageView.layer.masksToBounds = true
        imageView.layer.borderColor = UIColor.blackColor().CGColor
        imageView.layer.borderWidth = 1.0
        view1.addSubview(imageView)

        var label      = UILabel()
        let labelFrame = CGRect(x: 60.0, y: 5.0, width: 240.0, height: 40.0)
        label = UILabel(frame: labelFrame)
        label.text = contact["shopName"] as? String
        label.textColor = UIColor.grayColor()
        view1.addSubview(label)

        self.view.addSubview(view1)
    }

    func initLabel()
    {
        var label      = UILabel()
        let labelFrame = CGRect(x: 10, y: 60, width: 240.0, height: 40.0)
        label = UILabel(frame: labelFrame)
        label.text = "评价商品"
        label.textColor = UIColor.grayColor()
        label.font = UIFont.systemFontOfSize(12)
        self.view.addSubview(label)
    }

    func initView2()
    {
        let screen = UIScreen.mainScreen().bounds
        let view2  = UIView()
        view2.frame = CGRectMake(0, 100, screen.size.width, 200)
        view2.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(view2)

        let advertising    = comment["advertising"] as! [String:AnyObject]
        let thumbnailURL   = advertising["imageURL"] as? String
        var imageView      = UIImageView()
        let imageViewFrame = CGRect(x: 10.0, y: 5.0, width: 90.0, height: 90.0)
        imageView = UIImageView(frame: imageViewFrame)
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
        else
        {
            imageView.image = UIImage(named: "Avatar-Default.png")
        }
        imageView.layer.masksToBounds = true
        imageView.layer.borderColor = UIColor.grayColor().CGColor
        imageView.layer.borderWidth = 1.0
        view2.addSubview(imageView)

        let indexOfSaleTags = advertising["summary"] as? String
        var label           = UILabel()
        let labelFrame      = CGRect(x: 110.0, y: 5.0, width: 240.0, height: 40.0)
        label = UILabel(frame: labelFrame)
        label.text = "商品名称：" + indexOfSaleTags!
        label.textColor = UIColor.grayColor()
        view2.addSubview(label)

        button = UIButton(type: UIButtonType.System)
        button.frame = CGRectMake(110, 60, 40, 30)
        button.setTitle("一般", forState: .Normal)
        button.addTarget(self, action: "yiban", forControlEvents: .TouchUpInside)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel!.font = UIFont.systemFontOfSize(12)
        button.imageView?.contentMode = .ScaleAspectFit
        button.imageEdgeInsets = UIEdgeInsetsMake(5, -5, 5, 20)
        button.layer.borderWidth = 1.0
        button.layer.borderColor = UIColor.lightGrayColor().CGColor
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10
        view2.addSubview(button);

        button2 = UIButton(type: UIButtonType.System)
        button2.frame = CGRectMake(160, 60, 40, 30);
        button2.setTitle("不错", forState: .Normal)
        button2.addTarget(self, action: "bucuo", forControlEvents: .TouchUpInside)
        button2.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button2.titleLabel!.font = UIFont.systemFontOfSize(12)
        button2.imageView?.contentMode = .ScaleAspectFit
        button2.imageEdgeInsets = UIEdgeInsetsMake(5, -5, 5, 20)
        button2.layer.borderWidth = 1.0
        button2.layer.borderColor = UIColor.lightGrayColor().CGColor
        button2.layer.masksToBounds = true
        button2.layer.cornerRadius = 10
        view2.addSubview(button2);

//        var button3:UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton;
        button3 = UIButton(type: UIButtonType.System)
        button3.frame = CGRectMake(210, 60, 40, 30);
        button3.setTitle("超赞", forState: .Normal)
        button3.addTarget(self, action: "chaozan", forControlEvents: .TouchUpInside)
        button3.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button3.titleLabel!.font = UIFont.systemFontOfSize(12)
        button3.imageView?.contentMode = .ScaleAspectFit
        button3.imageEdgeInsets = UIEdgeInsetsMake(5, -5, 5, 20)
        button3.layer.borderWidth = 1.0
        button3.layer.borderColor = UIColor.lightGrayColor().CGColor
        button3.layer.masksToBounds = true
        button3.layer.cornerRadius = 10
        view2.addSubview(button3);

        button4 = UIButton(type: UIButtonType.System)
        button4.frame = CGRectMake(260, 60, 40, 30);
        button4.setTitle("神作", forState: .Normal)
        button4.addTarget(self, action: "shenzuo", forControlEvents: .TouchUpInside)
        button4.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button4.titleLabel!.font = UIFont.systemFontOfSize(12)
        button4.imageView?.contentMode = .ScaleAspectFit
        button4.imageEdgeInsets = UIEdgeInsetsMake(5, -5, 5, 20)
        button4.layer.borderWidth = 1.0
        button4.layer.borderColor = UIColor.lightGrayColor().CGColor
        button4.layer.masksToBounds = true
        button4.layer.cornerRadius = 10
        view2.addSubview(button4);

        let commentTextFieldFrame = CGRect(x: 10, y: 120, width: screen.size.width - 20, height: 30)
        commentTextField = UITextField(frame: commentTextFieldFrame)
        commentTextField.borderStyle = UITextBorderStyle.RoundedRect
        commentTextField.placeholder = "您的点评非常重要"
        commentTextField.font = UIFont.systemFontOfSize(12)
        commentTextField.adjustsFontSizeToFitWidth = true
        commentTextField.minimumFontSize = 8
        commentTextField.delegate = self
        view2.addSubview(commentTextField)

        var label2      = UILabel()
        let label2Frame = CGRect(x: screen.size.width - 50, y: 160.0, width: 40, height: 20)
        label2 = UILabel(frame: label2Frame)
        label2.text = "100"
        label2.textColor = UIColor.grayColor()
        view2.addSubview(label2)

    }

    func initRightBarButtonItem()
    {
        let button: UIButton = UIButton(type: UIButtonType.System) ;
        button.frame = CGRectMake(0, 0, 40, 30)
        button.setTitle("完成", forState: UIControlState.Normal)
        button.setTitleColor(UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1), forState: UIControlState.Normal)
        button.addTarget(self, action: "finish", forControlEvents: .TouchUpInside)
        let item = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = item
    }

    @IBAction func finish()
    {
        reviewAppointment["review"] = commentTextField.text as NSString!
        let message = commentTextField.text as NSString!


        let length = message.length
        if length > 200
        {
            self.alertWarningMessage("你填写的信息超过200字.")
            return
        }
        
        let filter = SensitiveWordsFilter()
        let valid: Bool = filter.validating(message as String)
        if !valid
        {
            self.alertWarningMessage("你填写的信息可能包含敏感内容.")
            return
        }
        if message.length > 0
        {
            if reviewAppointment["score"] == nil
            {
                reviewAppointment["score"] = "-1"
            }
            //let advertising = comment["advertising"] as! [String:AnyObject]

            let score  = reviewAppointment["score"] as? String
            let review = reviewAppointment["review"] as? String

            let id:            Int    = (comment["id"] as? Int)!
            let appointmentId: String = String(id)

            let rs = appointmentManager.reviewAppointment(appointmentId, score: score, review: review)

            if rs == nil
            {
                self.alertNetworkConnectionException()
                return
            }

            if rs!.hasErrors()
            {
                if rs?.errorCode == 3020
                {
                    self.alertUserStatusException()
                }
                else
                {
                    self.alertErrorMessage(rs!.errorMessage)
                }
            }
            else
            {
                let controller = MyOrderViewController()
                self.pushViewController(controller)
            }
        }
        else
        {
            self.alertWarningMessage("请填写评论内容")
            return
        }



//        if appDelegate.isLogged()
//        {
//            var message = commentTextField.text as NSString
//            if message.length > 0
//            {
//                let commentManager = CommentManager()
//                let articleId = comment.objectForKey("id") as! Int
//                var rs = commentManager.addComment(String(articleId), comment: message as String)
//               if rs != nil
//                {
//                    if !rs!.hasErrors()
//                    {
//                        commentTextField.text = nil
//                        self.view.endEditing(true)
//                    }
//                    else if rs!.errorCode == 3010
//                    {
//                        alertUserStatusException()
//                    }
//                    else
//                    {
//                        alertErrorMessage(rs!.errorMessage)
//                    }
//                }
//                else
//                {
//                    alertNetworkConnectionException()
//                }
//            }
//            else
//            {
//                self.alertWarningMessage("请填写评论内容")
//                return
//            }
//            
//            self.view .endEditing(true)
//            self.navigationController?.popViewControllerAnimated(true)
//        }
//        else
//        {
//            login()
//        }

    }

    @IBAction func yiban()
    {
        button.backgroundColor = UIColor(red: 245 / 225, green: 154 / 225, blue: 47 / 225, alpha: 1)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button2.backgroundColor = UIColor.whiteColor()
        button3.backgroundColor = UIColor.whiteColor()
        button4.backgroundColor = UIColor.whiteColor()
        button2.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button3.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button4.setTitleColor(UIColor.grayColor(), forState: .Normal)
        reviewAppointment["score"] = "0"

    }

    @IBAction func bucuo()
    {
        button2.backgroundColor = UIColor(red: 122 / 225, green: 179 / 225, blue: 61 / 225, alpha: 1)
        button2.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.backgroundColor = UIColor.whiteColor()
        button3.backgroundColor = UIColor.whiteColor()
        button4.backgroundColor = UIColor.whiteColor()
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button3.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button4.setTitleColor(UIColor.grayColor(), forState: .Normal)
        reviewAppointment["score"] = "1"
    }

    @IBAction func chaozan()
    {
        button3.backgroundColor = UIColor(red: 215 / 225, green: 51 / 225, blue: 155 / 225, alpha: 1)
        button3.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button2.backgroundColor = UIColor.whiteColor()
        button.backgroundColor = UIColor.whiteColor()
        button4.backgroundColor = UIColor.whiteColor()
        button2.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button4.setTitleColor(UIColor.grayColor(), forState: .Normal)
        reviewAppointment["score"] = "2"
    }

    @IBAction func shenzuo()
    {
        button4.backgroundColor = UIColor(red: 224 / 225, green: 30 / 225, blue: 30 / 225, alpha: 1)
        button4.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button2.backgroundColor = UIColor.whiteColor()
        button3.backgroundColor = UIColor.whiteColor()
        button.backgroundColor = UIColor.whiteColor()
        button2.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button3.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        reviewAppointment["score"] = "3"
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
